/**
 * 
 */
package any.note.server;

import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.Validate;

import any.note.Note;

/**
 * @author keke
 * @version $Revision: 34 $
 */
public class MockNoteService implements NoteService {
    private static class ManagedNote {
        private Note note;
    }

    private final Map<String, ManagedNote> notes = new ConcurrentHashMap<String, ManagedNote>();

    /*
     * (non-Javadoc)
     * 
     * @see any.note.server.NoteService#createNewNote()
     */
    public synchronized Note createNewNote() {
        final String uuid = StringUtils.join(UUID.randomUUID().toString()
                .split("-"));
        final ManagedNote managedNote = new ManagedNote();

        managedNote.note = new Note(uuid);
        managedNote.note.setStatus(Note.Status.NEW);
        notes.put(managedNote.note.getUuid(), managedNote);
        return managedNote.note;
    }

    /*
     * (non-Javadoc)
     * 
     * @see any.note.server.NoteService#delete(java.lang.String)
     */
    public synchronized void delete(final String noteId) {
        notes.remove(noteId);
    }

    /*
     * (non-Javadoc)
     * 
     * @see any.note.server.NoteService#getNote(java.lang.String)
     */
    public synchronized Note getNote(final String noteId) {
        final ManagedNote mNote = notes.get(noteId);
        return mNote == null ? null : mNote.note;
    }

    /*
     * (non-Javadoc)
     * 
     * @see any.note.server.NoteService#listAll()
     */
    public synchronized List<Note> listAll() {
        return null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see any.note.server.NoteService#save(any.note.Note)
     */
    public synchronized NoteService save(final Note entity) {
        Validate.notNull(entity);
        final ManagedNote mNote = notes.get(entity.getUuid());
        if (mNote != null) {
            mNote.note = entity;
            mNote.note.setStatus(Note.Status.SAVED);
        }
        return this;
    }

}
